<?php

namespace app\api\model;


use app\admin\model\ttk\Player;
use app\api\controller\User;

class TkGame
{


    private array $users;
    private array $players;

    public function __construct(int $count = 5)
    {
        /*根据人数，创建一组角色牌，一人一张。*/

        echo $count;
        TkUser::resetUserReady();
        if (TkUser::countReadyUser() >= $count) {
            /*创建游戏房间并开始发牌给玩家。*/
            echo '发现超过5人Ready，准备开启游戏进程' . PHP_EOL;
            $this->users = TkUser::getReadyUserQueue($count);
            TkUser::updateUserGo($this->users);
            /*检测如果全为机器人，暂时释放机器人。机器人自己对战*/

            $tkRoom = new TkRoom($this->users);

            $roles = TkCard::getRoleList($count);
            echo '生成乱序角色数组:' . join(',', $roles) . PHP_EOL;
            $pos = 0;
            /*跟据不同人数，生成乱序角色数组，然后下面分配给玩家*/
            $this->players = [
                'R' . Enum::Role_ZhuGong => [],
                'R' . Enum::Role_ZhoCen => [],
                'R' . Enum::Role_Faze => [],
                'R' . Enum::Role_JiCen => [],
            ];
            foreach ($this->users as $user) {
                $player = TkPlayer::createPlayer($tkRoom->room['id'], $user, $pos + 1, $roles[$pos]);
                array_push($this->players['R' . $roles[$pos]], $player);
                $pos++;
            }

            /*检测是否分配给players*/
            foreach ($this->players as $key => $role) {
                echo 'players数组:' . $key . '=' . sizeof($role) . PHP_EOL;
            }

            /*随机2个武将合并三雄。随机分配一张给主公玩家*/
            /*随机3个武将。随机分配一张给其他玩家*/



        }

    }


    public static function makeCards(int $count): array
    {
        if ($count == 5) {
            return ['code' => 1, 'data' => ['主公', '忠臣', '反贼', '反贼', '奸臣']];
        }
        return ['code' => 1, 'msg' => '不支持其他人数'];
    }


}